package com.onetime.mall.trading.controller;

import com.onetime.mall.trading.constants.ErrorMsg;
import com.onetime.mall.trading.entity.AddressEntity;
import com.onetime.mall.trading.service.AddressService;
import com.onetime.mall.trading.utils.PageUtils;
import com.onetime.mall.trading.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 地址表
 *
 * @author liyixuan
 * @email liyixuan106@163.com
 * @date 2022-03-26 19:38:29
 */
@RestController
@RequestMapping("address")
public class AddressController {
    @Autowired
    private AddressService addressService;


    @PostMapping("/add")
    public R addAddress(@RequestParam("userId")
                        @NotNull(message = "登录异常 请重新登录")
                        @NotEmpty(message = "登录异常 请重新登录") String shUserId,
                        @RequestBody AddressEntity addressEntity) {
        Long userId = Long.valueOf(shUserId);
        addressEntity.setUserId(userId);
        if (addressService.addAddress(addressEntity)) {
            return R.ok().put("data", addressEntity);
        }
        return R.error(ErrorMsg.SYSTEM_ERROR.getCode(), ErrorMsg.SYSTEM_ERROR.getMsg());
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = addressService.queryPage(params);

        return R.ok().put("page", page);
    }

    @GetMapping("/info")
    public R getAddress(@RequestParam("userId")
                        @NotNull(message = "登录异常 请重新登录")
                        @NotEmpty(message = "登录异常 请重新登录") String shUserId,
                        @RequestParam(value = "id", required = false) Long id) {
        Long userId = Long.valueOf(shUserId);
        if (null == id) {
            List<AddressEntity> list = addressService.getAddressByUser(userId);
            return R.ok().put("data", list);
        } else {
            AddressEntity address = addressService.getAddressById(id, userId);
            return R.ok().put("data", address);
        }
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        AddressEntity address = addressService.getById(id);

        return R.ok().put("address", address);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AddressEntity address) {
        addressService.save(address);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update1")
    public R update(@RequestBody AddressEntity address) {
        addressService.updateById(address);

        return R.ok();
    }

    @PostMapping("/update")
    public R updateAddress(@RequestParam("userId")
                           @NotNull(message = "登录异常 请重新登录")
                           @NotEmpty(message = "登录异常 请重新登录") String shUserId,
                           @RequestBody AddressEntity addressEntity) {
        Long userId = Long.valueOf(shUserId);
        addressEntity.setUserId(userId);
        System.out.println(addressEntity);
        if (addressService.updateAddress(addressEntity)) {
            return R.ok();
        }
        return R.error(ErrorMsg.SYSTEM_ERROR.getCode(), ErrorMsg.SYSTEM_ERROR.getMsg());
    }


    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        addressService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @PostMapping("/delete")
    public R deleteAddress(@RequestParam("userId")
                           @NotNull(message = "登录异常 请重新登录")
                           @NotEmpty(message = "登录异常 请重新登录") String shUserId,
                           @RequestBody AddressEntity addressEntity) {
        Long userId = Long.valueOf(shUserId);
        addressEntity.setUserId(userId);
        if (addressService.deleteAddress(addressEntity)) {
            return R.ok();
        }
        return R.error(ErrorMsg.SYSTEM_ERROR.getCode(), ErrorMsg.SYSTEM_ERROR.getMsg());
    }


}
